Guest User

dual pwm output option 2

a guest
Sep 17th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <avr/io.h>
  2.  
  3. #define POT_PIN 0 // Analog input pin for POT
  4. #define ENABLE_PIN 2 // GPIO input pin for enable/disable
  5. #define PWM1_PIN 1 // PWM output pin 1
  6. #define PWM2_PIN 3 // PWM output pin 2
  7.  
  8. int potValue = 0;
  9. int softStartValue = 0;
  10. int pwm1Value = 0;
  11. int pwm2Value = 0;
  12. bool isEnabled = false;
  13. unsigned long softStartTime = 0;
  14.  
  15. void setup() {
  16. pinMode(ENABLE_PIN, INPUT_PULLUP);
  17. pinMode(PWM1_PIN, OUTPUT);
  18. pinMode(PWM2_PIN, OUTPUT);
  19. analogReference(DEFAULT);
  20. }
  21.  
  22. void softStart() {
  23. unsigned long currentTime = millis();
  24. if (currentTime - softStartTime < 1000) {
  25. int softStartPercentage = map(currentTime - softStartTime, 0, 1000, 0, 100);
  26. pwm1Value = map(softStartPercentage, 0, 100, 20, softStartValue);
  27. } else {
  28. pwm1Value = softStartValue;
  29. }
  30. }
  31.  
  32. void loop() {
  33. isEnabled = digitalRead(ENABLE_PIN);
  34.  
  35. if (isEnabled) {
  36. potValue = analogRead(POT_PIN);
  37. softStartValue = map(potValue, 0, 1023, 0, 100);
  38.  
  39. softStart();
  40.  
  41. if (potValue <= 511) {
  42. pwm2Value = 0;
  43. digitalWrite(PWM2_PIN, LOW);
  44. } else {
  45. pwm2Value = map(potValue, 512, 1023, 0, 100);
  46. digitalWrite(PWM2_PIN, HIGH);
  47. }
  48.  
  49. analogWrite(PWM1_PIN, map(pwm1Value, 0, 100, 51, 255)); // Map to 51-255 (20%-100%)
  50. analogWrite(PWM2_PIN, map(pwm2Value, 0, 100, 51, 255)); // Map to 51-255 (20%-100%)
  51. } else {
  52. digitalWrite(PWM1_PIN, LOW);
  53. digitalWrite(PWM2_PIN, LOW);
  54. }
  55. }
  56.  
Add Comment
Please, Sign In to add comment